package com.lucy.nepu.module.topic.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Validator;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.nepu.loader.PeriodLoader;
import com.lucy.nepu.module.htopic.entity.HorTopic;
import com.lucy.nepu.module.htopic.params.HorTopicQueryParams;
import com.lucy.nepu.module.topic.entity.Topic;
import com.lucy.nepu.module.topic.event.TopicCreatedEvent;
import com.lucy.nepu.module.topic.lib.TopicDict;
import com.lucy.nepu.module.topic.lib.TopicDictService;
import com.lucy.nepu.module.topic.model.ExtParams;
import com.lucy.nepu.module.topic.params.TopicCreateParams;
import com.lucy.nepu.module.topic.params.TopicQueryParams;
import com.lucy.nepu.module.topic.service.TopicService;
import com.lucy.nepu.utils.ValidUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;


@RestController
@RequestMapping("nepu")
@Api(tags = "纵向课题")
public class TopicController {
    @Autowired
    TopicService service;

    @Autowired
    TopicDictService dictService;

    @Autowired(required = false)
    PeriodLoader periodLoader;

    @Resource
    ApplicationContext context;

    @PostMapping("topics")
    @ApiOperation(value = "课题新建",notes = "")
    public ResponseDTO<Object> topicCreate(@RequestBody @Validated TopicCreateParams params){
        // 1. 表单校验
        String errMsg = ValidUtil.formValid(params);
        if (!Validator.isEmpty(errMsg)){
            return ResponseDTO.wrap(HttpStatus.BAD_REQUEST,errMsg);
        }
        // 2. 获取当前时间所在周期ID -- 自动拦截不在时间段内的提交
        String periodId = periodLoader.getPeriodId();
        // 3. 获取词典信息
        TopicDict dict = dictService.getOneUnwrap(params.getTopicDictId());
        // 4. 写入记录并赋值最大工作量
        Topic topic = new Topic();
        topic.setPeriodId(periodId);
        ExtParams extParams = new ExtParams();
        extParams.setDeptId(params.getDeptId());
        extParams.setDeptName(params.getDeptName());
        topic.setExtParams(extParams);
        return ResponseDTO.succData(
                service.saveOne(topic)
        );
    }

    @DeleteMapping("topics/{id}")
    @ApiOperation(value = "课题删除",notes = "")
    public ResponseDTO<Object> topicDelete(@PathVariable("id") String id){
        Topic topic = service.getOneUnwrap(id);
        if (topic.getStatus().equals(2)){
            return ResponseDTO.wrap(HttpStatus.FORBIDDEN,"锁定记录，无法操作");
        }
        service.deleteOne(id);
        return ResponseDTO.succMsg("删除成功");
    }


    @GetMapping("topics")
    @ApiOperation(value = "课题列表")
    public ResponseDTO<Object> topicListQuery(TopicQueryParams params){
        Sort sort;
        if("DESC".equalsIgnoreCase(params.getOrder())){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
        /*
        构建 Example
         */
        Topic o = new Topic();
        BeanUtil.copyProperties(params,o, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<Topic> example = Example.of(o);
        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @GetMapping("topics/{id}")
    @ApiOperation(value = "课题查询",notes = "")
    public ResponseDTO<Object> topicQuery(@PathVariable("id") String id){
        return ResponseDTO.succData(
                service.getOneUnwrap(id)
        );
    }
}
